/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.common.json;

import com.entity.Employee;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

/**
 *
 * @author Tuan Long
 */
public class EmployeeManagerJson {

    public static String getCurrentDatetime() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String current = sdf.format(date);
        return "" + current;
    }

    public static String getFormatDate(String birthdate) {
        String output = "";
        try {
            SimpleDateFormat format = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
            Date inputDate = format.parse(birthdate);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            output = sdf.format(inputDate);
        } catch (ParseException ex) {
            Logger.getLogger(EmployeeManagerJson.class.getName()).log(Level.SEVERE, null, ex);
        }
        return output;
    }

    public static String convertEmployeeInfoToJSONString(Employee em) {
        JSONObject message = new JSONObject();
        message.put("Username", em.getUsername());
        message.put("Password", em.getPassword());
        message.put("Email", em.getEmail());
        message.put("Fullname", em.getFullname());
        message.put("Gender", em.getGender());
        message.put("Birthdate", em.getBirthdate());
        message.put("Phone", em.getPhone());
        message.put("Mobilephone", em.getMobilephone());
        message.put("Address", em.getAddress());
        message.put("IdentityCard", em.getIdentityCard());
        message.put("JoinInDate", em.getJoinInDate());
        message.put("Position", em.getPosition());
        message.put("Departments", em.getDepartments());
        message.put("Groups", em.getGroup());
        message.put("Status", em.getStatus());
        message.put("Online", em.getOnline());
        message.put("Logged", em.getLogged());
        message.put("Avatar", em.getAvatar());
        return "" + message.toString();
    }

    public static Employee convertJsonStringToEmployeeObjec(String jsonstr) {
        Employee em = new Employee();
        JSONObject json = JsonDecoder(jsonstr);
        String Username = json.get("Username").toString();
        String Password = json.get("Password").toString();
        String Email = json.get("Email").toString();
        String Fullname = json.get("Fullname").toString();
        String Gender = json.get("Gender").toString();
        String Birthdate = json.get("Birthdate").toString();
        String Phone = json.get("Phone").toString();
        String Mobilephone = json.get("Mobilephone").toString();
        String Address = json.get("Address").toString();
        String IdentityCard = json.get("IdentityCard").toString();
        String JoinInDate = json.get("JoinInDate").toString();
        String Position = json.get("Position").toString();
        String Departments = json.get("Departments").toString();
        String Groups = json.get("Groups").toString();
        String Status = json.get("Status").toString();
        String Online = json.get("Online").toString();
        String Logged = json.get("Logged").toString();
        String Avatar = json.get("Avatar").toString();        
        em = new Employee(Username, Password, Email, Fullname, Gender, Birthdate, Phone, Mobilephone, Address, IdentityCard, JoinInDate, Position, Departments, Groups, Status, Online, Logged, Avatar);
        return em;
    }

    /**
     * @JsonModel.JsonDecoder : input is String values send by client
     * @param jsonstr
     * @return JSONObject
     */
    public static JSONObject JsonDecoder(String jsonstr) {
        try {
            JSONParser parser = new JSONParser();
            JSONObject json = (JSONObject) parser.parse(jsonstr);
            return json;
        } catch (org.json.simple.parser.ParseException ex) {
            Logger.getLogger(EmployeeManagerJson.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static Employee convertgetRegistersToEmployeeObj(String json) {
        Employee em = new Employee();
        JSONObject jsonobj = JsonDecoder(json);
        String username = jsonobj.get("username").toString();
        String fullname = jsonobj.get("fullname").toString();
        String avatar = jsonobj.get("avatar").toString();
        String position = jsonobj.get("position").toString();
        String department = jsonobj.get("department").toString();
        String logged = jsonobj.get("logged").toString();   
        em.setUsername(username);
        em.setFullname(fullname);
        em.setAvatar(avatar);
        em.setPosition(position);
        em.setDepartments(department);
        em.setLogged(logged);
        return em;
    }

}
